<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<progress id="progressBar" value="0" max="100" style="width: 300px;"></progress>
		<span id="percentage"></span><span id="time"></span>
		<br /><br />
		<input type="file" id="file" name="myfile" accept="image/x-png, image/jpg, image/jpeg, image/gif"/>
		<input type="button" onclick="UpladFile()" value="上传" />
		<input type="button" onclick="cancleUploadFile()" value="取消" />
		<script src="./js/ajax.js"></script>
		<script src="./js/conf.js"></script>
		<script src="./jquery.mobile-1.4.5/jquery-1.8.3.min.js"></script>
		<script src="js/image_compress.js"></script>
		<script type="text/javascript">
		   
		
		
		    var xhr,imgURL;
		    //上传文件方法
		    function UpladFile() {
		        var fileObj = document.getElementById("file").files[0]; // js 获取文件对象
		        var form = new FormData(); // FormData 对象
		
				if(fileObj.size/1024 > 1025) { //大于1M，进行压缩上传
					photoCompress(fileObj, {
					quality: 0.2
					}, function(base64Codes){
					//console.log("压缩后：" + base.length / 1024 + " " + base);
						var bl = convertBase64UrlToBlob(base64Codes);
						form.append("file", bl, "file_"+Date.parse(new Date())+".jpg"); // 文件对象
						uploadImg(form);
					});
				}else{ //小于等于1M 原图上传
					form.append("file", fileObj); // 文件对象
					uploadImg(form);
				}
			}
		
		    //上传成功响应
		    function uploadComplete(evt) {
		        //服务断接收完文件返回的结果
				console.log(evt);
		        var data = JSON.parse(evt.target.responseText);
		        if(data.success) {
		            alert("上传成功！");
		        }else{
		            alert("上传失败！");
		        }
		
		    }
		    //上传失败
		    function uploadFailed(evt) {
		        alert("上传失败！");
		    }
		    //取消上传
		    function cancleUploadFile(){
		        xhr.abort();
		    }
			
			//ajax上传到服务器端
			function uploadImg(form){
				$.ajax({
				type: "POST",
				url:backend_url + "/files/fileUpload.action",
				data:form, 
				cache: false,
				contentType: false,
				processData: false,
				async: false,
				success: function(data) {
					console.log("上传路径为："+data.url);
					imgURL=data.url;
				},error:function(data) {
					alert("上传失败，请检查网络设置或重试！");
					imgURL = "";
				}
				});
			}
			
		    //上传进度实现方法，上传过程中会频繁调用该方法
			function progressFunction(evt) {
		        var progressBar = document.getElementById("progressBar");
		        var percentageDiv = document.getElementById("percentage");
		        // event.total是需要传输的总字节，event.loaded是已经传输的字节。如果event.lengthComputable不为真，则event.total等于0
		        if (evt.lengthComputable) {//
		            progressBar.max = evt.total;
		            progressBar.value = evt.loaded;
		            percentageDiv.innerHTML = Math.round(evt.loaded / evt.total * 100) + "%";
		        }
		        var time = document.getElementById("time");
		        var nt = new Date().getTime();//获取当前时间
		        var pertime = (nt-ot)/1000; //计算出上次调用该方法时到现在的时间差，单位为s
		        ot = new Date().getTime(); //重新赋值时间，用于下次计算
		        var perload = evt.loaded - oloaded; //计算该分段上传的文件大小，单位b
		        oloaded = evt.loaded;//重新赋值已上传文件大小，用以下次计算
		        //上传速度计算
		        var speed = perload/pertime;//单位b/s
		        var bspeed = speed;
		        var units = 'b/s';//单位名称
		        if(speed/1024>1){
		            speed = speed/1024;
		            units = 'k/s';
		        }
		        if(speed/1024>1){
		            speed = speed/1024;
		            units = 'M/s';
		        }
		        speed = speed.toFixed(1);
		        //剩余时间
		        var resttime = ((evt.total-evt.loaded)/bspeed).toFixed(1);
		        time.innerHTML = '，速度：'+speed+units+'，剩余时间：'+resttime+'s';
		        if(bspeed==0) time.innerHTML = '上传已取消';
		    }
		</script>
	</body>
</html>